//
//  HistoryView.m
//  EPBPro
//
//  Created by wucha on 2018/6/26.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "HistoryView.h"
#import "HttpEquipmentAction.h"
#import "HistoryViewCell.h"
#import "MaintainInfo.h"

@interface HistoryView()<UITableViewDelegate,UITableViewDataSource>{
    HistoryBlock historyBlock;
}
@property (nonatomic,strong) NSArray *listArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *noResultView;
@end

@implementation HistoryView

-(id)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        UITableView *tableView = [UITableView new];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = 0;
        [self addSubview:tableView];
        tableView.sd_layout.leftSpaceToView(self, 0).topSpaceToView(self, 0).bottomSpaceToView(self, 0).widthIs(SCREEN_WIDTH);
        self.tableView = tableView;
    }
    return self;
}

-(void)loadData:(EquipmentInfo*)equipmentInfo block:(HistoryBlock)block{
    historyBlock = block;
    EquipmentRepairActionInfo *equipmentRepairActionInfo = [[EquipmentRepairActionInfo alloc] init];
    equipmentRepairActionInfo.equipmentId = equipmentInfo.id;
    [HttpEquipmentAction postEquipmentRepairListWithAction:equipmentRepairActionInfo block:^(id result, NSError *error) {
        if (!error) {
            self.listArray = result;
            [self.tableView reloadData];
            if (self.listArray.count == 0) {
                [self showNoResultWithFrame:self.bounds];
            }else{
                [self hiddenNoResult];
            }
        }
    }];
}

-(void)loadOtherData:(EquipmentInfo *)equipmentInfo{
    [HttpEquipmentAction postEquipmentRentHistory:equipmentInfo.id block:^(id result, NSError *error) {
        if (!error) {
            NSArray *array = result;
            NSMutableArray *mutableArray = [NSMutableArray array];
            for (NSDictionary *dic in array) {
                NSString *startDate = [Utils getTimeFront:dic[@"startDate"]];
                NSString *endDate = [Utils getTimeFront:dic[@"endDate"]];
                MaintainInfo *maintainInfo = [[MaintainInfo alloc] init];
                maintainInfo.startDate = dic[@"startDate"];
                maintainInfo.descriptions = [NSString stringWithFormat:@"%@\n%@-%@",dic[@"name"],[Utils replaceTimeString:startDate],[Utils replaceTimeString:endDate]];
                [mutableArray addObject:maintainInfo];
            }
            self.listArray = mutableArray;
            [self.tableView reloadData];
            
            if (self.listArray.count == 0) {
                [self showNoResultWithFrame:self.bounds];
            }else{
                [self hiddenNoResult];
            }
        }
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HistoryViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[HistoryViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    MaintainInfo *maintainInfo = self.listArray[indexPath.row];
    [cell setHistroyCell:maintainInfo];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:SCREEN_WIDTH tableView:tableView];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MaintainInfo *maintainInfo = self.listArray[indexPath.row];
    if (historyBlock) {
        historyBlock(maintainInfo.id,maintainInfo.status);
    }
}

-(void)showNoResultWithFrame:(CGRect)rect{
    if (_noResultView == nil) {
        int height = rect.size.height;
        UIView *view = [[UIView alloc] initWithFrame:rect];
        
        UIImageView *noImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-45, height/2-85, 90, 90)];
        noImageView.image = [UIImage imageNamed:@"noresult"];
        [view addSubview:noImageView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, noImageView.frameBottom+10, SCREEN_WIDTH, 20)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = [UIColor grayColor];
        [view addSubview:titleLabel];
        titleLabel.text = @"目前没有数据";
        [self addSubview:view];
        _noResultView = view;
    }
}

-(void)hiddenNoResult{
    _noResultView.hidden = YES;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
